import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";
import Cookies from 'js-cookie';


// 获取用户级别和活动ID
const userLevel =  Cookies.get('level');
const activityId =  Cookies.get('activity_id');

// 基本路由配置
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
    requiresAuth: false,
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true,
    meta: { title: '404 Not Found' },
  },
  {
    path: '/',
    component: Layout,
    redirect: '/setting',
    children: [
      {
        path: 'setting',
        name: 'Setting',
        component: () => import('@/views/setting/index'),
        meta: { title: '系统设置', icon: 'sys-setting' },
      },
    ],
  },
  {
    path: '/activity',
    component: Layout,
    redirect: '/activity/list',
    children: [
      {
        path: 'list',
        name: 'ActivityList',
        component: () => import('@/views/activity/index'),
        meta: { title: '活动列表', icon: 'active-list' },
      },
      {
        path: 'edit/:id(\\d+)',
        component: () => import('@/views/activity/edit'),
        name: 'EditActivity',
        meta: { title: '规则设置', noCache: true, icon: 'rule-setting' },
        hidden: true,
      },
    ],
  },
  {
    path: '/statistics',
    component: Layout,
    redirect: '/statistics/work',
    name: 'Statistics',
    meta: { title: '统计管理', icon: 'static-sys' },
    children: [
      {
        path: 'work',
        name: 'work-statistics',
        component: () => import('@/views/statistics/work/index'),
        meta: { title: '作品统计', icon: 'work-statis' },
      },
      {
        path: 'judges',
        name: 'judges-statistics',
        component: () => import('@/views/statistics/judges/index'),
        meta: { title: '评委统计', icon: 'judge' },
      },
      {
        path: '/statistics/work/detail',
        component: () => import('@/views/statistics/work/detail'),
        name: 'Detail',
        meta: { title: '明细', noCache: true },
        hidden: true,
      },
      {
        path: '/statistics/work/all-detail',
        component: () => import('@/views/statistics/work/detail-all'),
        name: 'AllDetail',
        meta: { title: '总明细', noCache: true },
        hidden: true,
      },
    ],
  },
  {
    path: '/account',
    component: Layout,
    redirect: '/account/roles',
    name: 'Account',
    meta: { title: '账户管理', icon: 'account-sys' },
    children: [
      {
        path: 'roles',
        name: 'account-roles',
        component: () => import('@/views/account/roles/index'),
        meta: { title: '角色管理', icon: 'role' },
      },
      {
        path: 'list',
        name: 'account-list',
        component: () => import('@/views/account/accountList/index'),
        meta: { title: '账号列表', icon: 'account-list' },
      },
    ],
  },
  // 前端路由
  {
    path: '/home',
    component: Layout,
    redirect: to => {
      return `/home/${to.params.level || userLevel}/${to.params.activity_id || activityId}`;
    },
    name: 'home',
    meta: { title: '社区主页' },
    hidden: true,
    children: [
      {
        path: 'level1/:activity_id',
        name: 'Level1',
        component: () => import('@/views/level1/index.vue'),
        meta: { title: 'Level 1' },
      },
      {
        path: 'level2/:activity_id',
        name: 'Level2',
        component: () => import('@/views/level2/index.vue'),
        meta: { title: 'Level 2' },
      },
      {
        path: 'level3/:activity_id',
        name: 'Level3',
        component: () => import('@/views/level3/index.vue'),
        meta: { title: 'Level 3' },
      },
      {
        path: 'search',
        name: 'Search',
        component: () => import('@/views/search/index.vue'),
        meta: { title: 'Search', noCache: true },
      },
      {
        path: '*',
        name: '404',
        component: () => import('@/views/404'),
        meta: { title: '404' },
      },
    ],
  },
];

const createRouter = () =>
  new Router({
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;